<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Company_comment_model extends CI_Model {
    public $table = 'company_comments';
    public $primary = 'id';
    
    function add($data) {
        $this->db->insert($this->table, $data);
    }
    
    function getCommentByCompanyId($company_id, $start, $limit) {
        $query = $this->db->query("SELECT {$this->table}.*, `users`.`avatar` AS `avatar`,
                                    `users`.`real_name` as `real_name`
                                    FROM {$this->table}, `users` 
                                    WHERE `company_id` = '{$company_id}'
                                    AND {$this->table}.`user_id` = `users`.`user_id`
                                    ORDER BY `commented_time` DESC 
                            	    LIMIT {$start}, {$limit}");
        return $query->result_array();
    }
    
    function getLastCommentByCompanyId($company_id, $user_id) {
        $query = $this->db->query("SELECT {$this->table}.*, `users`.`avatar` AS `avatar`,
                                    `users`.`real_name` as `real_name`
                                    FROM {$this->table}, `users` 
                                    WHERE {$this->primary} = (SELECT MAX({$this->primary}) 
                                                    FROM {$this->table} 
                                                    WHERE `company_id` = '{$company_id}'
                                                    AND `user_id` = '{$user_id}') 
                                    AND `company_id` = '{$company_id}'
                                    AND {$this->table}.`user_id` = `users`.`user_id`");
        return $query->result_array();
    }
    
    function getLastCommentByUserIdAndCompanyId($user_id, $company_id) {
        $query = $this->db->query("SELECT * FROM {$this->table} 
                                    WHERE {$this->primary} = (SELECT MAX({$this->primary}) 
                                                    FROM {$this->table} 
                                                    WHERE `company_id` = '{$company_id}'
                                                    AND `user_id` = '{$user_id}')");
        return $query->row_array();
    }
    
    function getCountCommentByCompanyId($company_id) {
        $query = $this->db->query("SELECT COUNT({$this->primary}) AS `count` FROM {$this->table} 
                                    WHERE `company_id` = '{$company_id}'
                                    ");
        return $query->row_array();
    }
}
?>
